using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PicoInterface;

namespace RapidHDL
{
    public partial class FSignals : Form
    {
        HardwareTreeVeiw oHardwareTreeView;
        RapidHardware oRapidHardware;

        public FSignals()
        {
            InitializeComponent();
        }

        SNNMaster oSNN; 

        public void Initialize(RapidHardware poRapidHardware)
        {
            oRapidHardware = poRapidHardware;            
            
            SNNHardware oHardware;
            try
            {
                oHardware = (SNNHardware)oRapidHardware;
            }
            catch
            {
                oHardware = null;
            }
            if (oHardware != null)
                oSNN = oHardware.SNN;
            ctlChip.AttachMicroTrace(ctlTrace);

            
            ctlChip.RapidHardware = poRapidHardware;

            tvHardware.ImageList = imgList;
            TreeNode tnRoot = tvHardware.Nodes.Add("Root","Root",22);
            oHardwareTreeView = new HardwareTreeVeiw(oRapidHardware, tvHardware,tvHardware.Nodes[0],23);
            oHardwareTreeView.ReloadTree();

            tnRoot.Expand();
            oHardwareTreeView.ExpandSignalView();

            if (oSNN != null)
            {
                ctlTrace.SetAddressToLine(oSNN.GetProgramAddressToLine());
                ctlTrace.SetText(oSNN.GetProgramText());
                oSNN.LoadRAMProgram();
            }
        }

        private void FSignals_Load(object sender, EventArgs e)
        {            
        }

        private void tvHardware_AfterSelect(object sender, TreeViewEventArgs e)
        {
            SelectHardwareNode(tvHardware.SelectedNode);
        }

        private void ctlChip_Load(object sender, EventArgs e)
        {
            ctlChip.AttachMicro("core_CORE_state_ir", "PC_OUT");
        }

        private void ctlChip_Updated()
        {
            //if (oHardwareTreeView != null)
            //    oHardwareTreeView.RefreshTree();
            tmrTranscript_Tick(null, null);
        }


        private void SelectHardwareNode(TreeNode ptnNode)
        {
            bool bShowProperties = true;
            RapidHDL.SelectedHardwareType selectedType = oHardwareTreeView.GetSelectedHardwareType(ptnNode);
            object oSelected = ptnNode.Tag;

            switch (selectedType)
            {
                case SelectedHardwareType.Component:
                    break;
                case SelectedHardwareType.Root:
                    break;
                case SelectedHardwareType.NVPassthrough:
                    break;
                case SelectedHardwareType.NVSink:
                    break;
                case SelectedHardwareType.NVSource:
                    break;
                case SelectedHardwareType.TopHardware:
                    break;
                case SelectedHardwareType.View:
                    break;
                case SelectedHardwareType.Source:
                    break;
                case SelectedHardwareType.Verilog:
                    cbTrace.Checked = false;
                    cbTranscript.Checked = false; 
                    ComponentVerilog oVerilog = (ComponentVerilog)oSelected;
                    ctlTextView.LoadVerilog(oVerilog);
                    break;
                case SelectedHardwareType.Log:
                    cbTrace.Checked = false;
                    cbTranscript.Checked = false;
                    ctlTextView.LoadLogFile((string)oSelected);
                    break;
                default:
                    bShowProperties = false;
                    break;
            }

            if (bShowProperties)
            {
                pgView.SelectedObject = oSelected;
            }
        }

        private void tmrTranscript_Tick(object sender, EventArgs e)
        {
            if (cbTranscript.Checked)
            {
                if (oRapidHardware.Transcript.Changed)
                {
                    ctlTextView.ShowTranscript(oRapidHardware.Transcript.GetTranscript());
                }
            }
        }

            
        private void button1_Click(object sender, EventArgs e)
        {
            TestPCSDRAM.PCTest();
            //TestPCIO.PCIOPerSec();
            //TestFIFOInterface.PCTestPost();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IOMemoryTest.TestMemory(1000000, 1000600);
            IOMemoryTest.TestMemory(2000000,2500000);
            IOMemoryTest.TestMemory(200, 300);
            for (int iBlock = 0; iBlock < 30; iBlock++)
            {
                int iStart = iBlock * 1000000 + 100000;
                IOMemoryTest.TestMemory(iStart, iStart + 100);
            }
            IOMemoryTest.TestMemory(4194300, 4194400);
            IOMemoryTest.TestMemory(4000000, 10000000);
            IOMemoryTest.TestMemory(200, 300);
            
            //TestPCIO.PCTest();
            //TestFIFOInterface.PCTestRead();
            //TestFIFOInterface.PCTest();
            //MessageBox.Show(Conversion.IntToBinaryString(PicoIO.ReadIOStatusReg()));
        }

        private void cbTrace_CheckedChanged(object sender, EventArgs e)
        {
            cbTranscript.Checked = !cbTrace.Checked;
            if (cbTrace.Checked)
            {
                ctlTrace.Visible = true;

            }
            else
            {
                ctlTrace.Visible = false;
            }
        }

        private void cmdLoadProgram_Click(object sender, EventArgs e)
        {
            IOSpikingNetwork.ConnectSynapse(2, 3, true);
        }

        private void cmdStartRAM_Click(object sender, EventArgs e)
        {
            IOSpikingNetwork.MainReset();

            /*FPGABlock oBlock = new FPGABlock();
            oBlock.LoadA(33);
            IOInterface.PostFPGA(oBlock);*/


            //if (!oSNN.StartRAMOnFPGA())
            //    System.Windows.Forms.MessageBox.Show("ERROR starting SNN RAM Program");
            //else
            //oSNN.LoadRAMProgram();
            //    System.Windows.Forms.MessageBox.Show("OK");
        }

        private void cmdCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(oRapidHardware.Transcript.GetTranscript());
        }



    }
}